package com.z.dao;

import java.util.ArrayList;
import java.util.List;

import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;
import com.z.base.DbBase;
import com.z.entity.FileKeyWord;

public class FileKeyWordDao extends DbBase {
	private static FileKeyWordDao fileKeyWordDao;
	public static FileKeyWordDao getInstace(){
		if(fileKeyWordDao==null){
			fileKeyWordDao=new FileKeyWordDao();
		}
		return fileKeyWordDao;
	}
	
	@SuppressWarnings("serial")
	public String[] findKeyWords(final String fileId){
		ObjectContainer db = null;
		String[] words=null;
		try {
			db = getDB();
			List<FileKeyWord> fileKeyWordList =db.query(new Predicate<FileKeyWord>() {
				@Override
				public boolean match(FileKeyWord fileKeyWord) {
					return fileKeyWord.getUploadFileId().equals(fileId);
				}
			});
			if(fileKeyWordList!=null&&fileKeyWordList.size()>0){
				words=new String[fileKeyWordList.size()];
				for(int i=0;i<fileKeyWordList.size();i++){
					words[i]=fileKeyWordList.get(i).getKeyWord();
				}
			}
		} catch (Exception e) {
		}
		return words;
	}

	@SuppressWarnings("serial")
	public List<String> matchUploadFileId(final String param){
		ObjectContainer db = null;
		List<String> list=new ArrayList<String>();
		try {
			db = getDB();
			List<FileKeyWord> fileKeyWordList =db.query(new Predicate<FileKeyWord>() {
				@Override
				public boolean match(FileKeyWord fileKeyWord) {
					return fileKeyWord.getKeyWord().contains(param);
				}
			});
			for(FileKeyWord fileKeyWord:fileKeyWordList){
				list.add(fileKeyWord.getUploadFileId());
			}
		} catch (Exception e) {
		}
		return list;
	}
}
